package com.gold.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.stereotype.Service;

import com.gold.entity.Game;
import com.gold.entity.GameType;
import com.gold.mapper.GameMapper;
import com.gold.mapper.GameTypeMapper;
import com.gold.myexception.Myexception;
import com.gold.service.GameService;
import com.gold.service.GameTypeService;
import com.gold.service.StatusService;
import com.gold.util.Myutil;

@Service
public class GameServiceImpl implements GameService {

	@Autowired
	private GameMapper gm;
	@Autowired
	private GameTypeMapper gtm;

	public List<Game> selectAllGame() {
		return gm.selectAllGame();
	}

	public List<Game> selectAllGameByPage(Map<String, String> map) {
		int pageSize = 3;
		int userCount = gm.selectAllGame().size();
		int pageCount = userCount % pageSize == 0 ? userCount / pageSize : userCount / pageSize + 1;
		String nowPageString = map.get("nowPageString");
		if (nowPageString == null) {
			nowPageString = "1";
		}
		int nowPage = Integer.parseInt(nowPageString);
		if (nowPage <= 0) {
			nowPage = pageCount;
		}
		if (nowPage > pageCount) {
			nowPage = 1;
		}
		map.put("start", "" + ((nowPage - 1) * pageSize + 1));
		map.put("end", "" + (nowPage * pageSize));
		map.put("nowPageString", nowPage + "");
		return gm.selectAllGameByPage(map);
	}

	public void deleteGame(String[] gameIdStringArr) {
		for (String gameIdString : gameIdStringArr) {
			int gameId = Integer.parseInt(gameIdString);
			gm.deleteGame(gameId);
		}
	}

	public void insertGame(Game game) throws Myexception {
		Game oldGame = gm.selectGameByGameName(game.getGameName());
		if (oldGame != null) {
			throw new Myexception("游戏名称已经存在");
		}
		/*GameType gametype = gtm.selectGameTypeByTypeId(game.getGameTypeId());
		if (gametype != null && gametype.getTypeStatusId() == 2) {
			throw new Myexception("游戏类型已下线");
		}*/
		gm.insertGame(game);
	}

	public void updateGame(Game game, CommonsMultipartFile coverFile, CommonsMultipartFile... screenFile) throws Myexception {
		Game oldGame = gm.selectGameByGameId(game.getGameId());
		if (!Myutil.checkStringNull(game.getGameName())) {
			throw new Myexception("游戏名称不能为空");
		}
		if (!oldGame.getGameName().equals(game.getGameName())) {
			Game ortherGame = gm.selectGameByGameName(game.getGameName());
			if (ortherGame != null) {
				throw new Myexception("游戏名称不可重复");
			}
		}
		String coverFilePath = "/home/soft01/upload/game/";
		String screenFilePath = "/home/soft01/upload/game/screen/";
		String coverFileName = coverFile.getOriginalFilename();
		if (!"".equals(coverFileName)) {
			String coverFileNewName = Myutil.getDateString() + coverFileName.substring(coverFileName.lastIndexOf("."));
			game.setGameCover(coverFileNewName);
			try {
				coverFile.transferTo(new File(coverFilePath + coverFileNewName));
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			game.setGameCover(oldGame.getGameCover());
		}
		String[] oldScreenFileName = oldGame.getGameScreen().split("[*]");
		for (int i = 0; i < screenFile.length; i++) {
			String screenFileName = screenFile[i].getOriginalFilename();
			if (!"".equals(screenFileName)) {
				String screenFileNewName = i + "-" + Myutil.getDateString() + screenFileName.substring(screenFileName.lastIndexOf("."));
				try {
					screenFile[i].transferTo(new File(screenFilePath + screenFileNewName));
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				oldScreenFileName[i] = screenFileNewName;
			}
		}
		game.setGameScreen(oldScreenFileName[0] + "*" + oldScreenFileName[1] + "*" + oldScreenFileName[2]);
		gm.updateGame(game);
	}

	public Game selectGameByGameId(int gameId) {
		return gm.selectGameByGameId(gameId);
	}

	public List<Game> selectGameByLike(Map<String, String> map) {
		return gm.selectGameByLike(map);
	}

	public List<Game> selectGameByPortal() {
		return gm.selectGameByPortal();
	}

	public List<Game> selectGameByGameType(int gameType) {
		return gm.selectGameByGameType(gameType);
	}

	public List<Game> selectGameByGameTypeAndName(Map<String, String> map) {
		return gm.selectGameByGameTypeAndName(map);
	}

}
